< previous page page_157 next page >

Page 157
First, use the printer's ScaleX and ScaleY methods to get the printer's dimensions in twips. Then use the form's ScaleX and ScaleY methods to translate these dimensions into the form's coordinate system. This gives the size of the printer's printable area in the form's coordinate system. You will use these dimensions for the printer's new ScaleWidth and ScaleHeight values.
Next, determine where the center of the form is in the form's coordinate system. If you use this point as the center of the printable area, the form's drawing area will be centered.
Now you can compute scaled coordinate values for the printer's upper-left and lower-right corners. Use these in the printer's Scale method to set the scaling properties, and the printer is ready to go.
The SetPrinterScale subroutine shown in the following code uses these steps to make the printer's scale properties match those of a form or picture box.
e10a6a0a59f035e870d90cca05b85fce.gif
' Set the printer's scale properties so it will
' print the object at the correct size, centered
' in the printable area.
Private Sub SetPrinterScale(obj As Object)
Dim pwid As Single
Dim phgt As Single
Dim xmid As Single
Dim ymid As Single

    ' Get the printer's dimensions in twips.
    pwid = Printer.ScaleX(Printer.ScaleWidth, _
        Printer.ScaleMode, vbTwips)
    phgt = Printer.ScaleY(Printer.ScaleHeight, _
        Printer.ScaleMode, vbTwips)

    ' Convert the printer's dimensions into the
    ' object's coordinates.
    pwid = obj.ScaleX(pwid, vbTwips, obj.ScaleMode)
    phgt = obj.ScaleY(phgt, vbTwips, obj.ScaleMode)

    ' Compute the center of the object.
    xmid = obj.ScaleLeft + obj.ScaleWidth / 2
    ymid = obj.ScaleTop + obj.ScaleHeight / 2

    ' Pass the coordinates of the upper left and
    ' lower right corners into the Scale method.
    Printer.Scale _
        (xmid - pwid / 2, ymid - phgt / 2)- _
        (xmid + pwid / 2, ymid + phgt / 2)
End Sub
Example program FrmPrint uses the SetPrinterScale subroutine to display and print a Bowditch curve (also called a curve of Lissajous). When you resize the form, the program resizes the curve to make it as large as possible. When you invoke the File menu's

 
< previous page page_157 next page >